    
import asyncio
import aio_pika
import json
#import aiomysql

async def process_task_with_mysql(task_data):
    # 连接MySQL
    print("task_data",task_data)

async def on_message(message: aio_pika.IncomingMessage):
    async with message.process():
        task_data = json.loads(message.body.decode())
        result = await process_task_with_mysql(task_data)
        #print(f"Processed task {task_data['task_id']}: {result}")

async def worker():
    connection = await aio_pika.connect_robust("amqp://guest:guest@localhost/")
    channel = await connection.channel()
    queue = await channel.declare_queue("api_requests", durable=True)
    await queue.consume(on_message)
    
    print("Worker started. Waiting for messages...")
    await asyncio.Future()  # run forever

if __name__ == "__main__":
    asyncio.run(worker())